package cn.xspace.app.filemg.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.xspace.android.xafcommon.imgload.core.DisplayImageOptions;
import com.xspace.android.xafcommon.imgload.core.assist.ImageScaleType;
import com.xspace.android.xafcommon.imgload.core.display.FadeInBitmapDisplayer;
import com.xspace.android.xafcommon.util.BaseRProxy;
import com.xspace.android.xafcommon.util.ImageUtils;
import com.xspace.android.xafcommon.util.StringUtils;

import java.util.ArrayList;

import cn.xspace.app.filemg.bean.FileInfo;
import cn.xspace.app.filemg.util.CallBack;

public class ImgGridAdapter extends ArrayAdapter<FileInfo> {
	private Context context;
	private DisplayImageOptions options;
	  private CallBack callBack;
	public ImgGridAdapter(Context context, ArrayList<FileInfo> objects,CallBack callBack) {
		super(context, 0, objects);
		this.context = context;
		options = new DisplayImageOptions.Builder().showImageForEmptyUri(BaseRProxy.picturndefault1).showImageOnFail(BaseRProxy.picturndefault2).resetViewBeforeLoading().cacheInMemory()
				.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(300)).build();
	    this.callBack=callBack;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(BaseRProxy.img_grid_item, null);
			viewHolder = new ViewHolder(convertView);
		
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final FileInfo info = getItem(position);
		if (!TextUtils.isEmpty(info.getPath())) {
			ImageUtils.imageLoader.displayImage(
					StringUtils.getUri(info.getPath()), viewHolder.image,
					options);
		}
		viewHolder.mCheckBox.setChecked(info.isCheckFlag());
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				callBack.checkedInfo(info);
			}
		});
		return convertView;
	}

	class ViewHolder {
		public ImageView image;
		public CheckBox mCheckBox;
		public ViewHolder(View v){
			mCheckBox = (CheckBox) v.findViewById(BaseRProxy.box);
			image = (ImageView) v.findViewById(BaseRProxy.ItemImage);
		}
	}
}
